#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""GIT account status logic

The logic to handle the account status checking request. It collects the input
parameters and the current account status then make decision to call the action
class to generate the response.
"""

__author__ = 'Xin Zhou(zhounew@google.com)'

from git.data import git_account
from git.util import git_context
from git.util import git_util

class GitAccountStatusLogic:
  def __init__(self, action):
    self.action_ = action

  def Run(self, request, response):
    email = request.GetEmail()
      
    if not email and not git_util.GitUtil.IsValidEmail(email):
      self.action_.SendError(request, response)
    
    service = git_context.GitContext.GetAccountService()
    account = service.GetAccountByEmail(email)
    
    if account:
      request.SetAccount(account)
      if account.GetAccountType() == git_account.GitAccount.FEDERATED:
        self.action_.SendRegisteredFederated(request, response)
      else:
        self.action_.SendRegisteredLegacy(request, response)
    else:
      can_federate = git_util.GitUtil.IsFederatedDomain(
          git_util.GitUtil.GetEmailDomain(email))
      if can_federate:
        self.action_.SendUnRegisteredFederated(request, response)
      else:
        self.action_.SendUnRegisteredLegacy(request, response)